package com.roamer.rest.exception;


import com.roamer.rest.IErrorCode;
import com.roamer.util.StringUtils;
import lombok.Getter;

import static com.roamer.rest.ResultCode.ErrorCode;

/**
 * 基础异常
 *
 * @author roamer
 * @version V1.0
 * @date 2019/7/16 20:09
 */
@Getter
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -8464756557217304892L;

    protected ErrorCode errorCode;

    protected int code;

    protected String message;

    protected Object data;

    private BusinessException(String format, Object[] argArray,
                              Throwable cause) {
        this();
        this.initCause(cause);
        this.message = StringUtils.formatIfArgs(format, argArray);
    }

    public BusinessException() {
        errorCode = IErrorCode.ERROR;
        code = errorCode.getCode();
        message = errorCode.getMessage();
    }

    public BusinessException(String msg) {
        this(msg, (Object) null);
    }

    public BusinessException(String format, Object arg) {
        this(format, new Object[]{arg}, null);
    }

    public BusinessException(String format, Object arg1, Object arg2) {
        this(format, new Object[]{arg1, arg2}, null);
    }

    public BusinessException(String format, Object... args) {
        this(format, args, null);
    }

    public BusinessException(ErrorCode errorCode) {
        this(errorCode, null);
    }

    public BusinessException(ErrorCode errorCode, Object data) {
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.message = errorCode.getMessage();
        this.data = data;
    }


}
